package com.yjc.rabbitmq.springboot_rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shkstart
 * @create 2022-08-02 22:25
 */
@Configuration
public class DelayedQueueConfig {

    //延迟队列
    public static final String DELAYED_QUEUE = "delayed.queue";
    //延迟交换机
    public static final String DELAYED_EXCHANGE = "delayed.exchange";
    //延迟RoutingKey
    public static final String DELAYED_ROUTINGKEY = "delayed.routingkey";

    @Bean("delayedQueue")
    public Queue delayedQueue() {
        return new Queue(DELAYED_QUEUE);
    }

    @Bean("delayedExchange")
    public CustomExchange delayedExchange() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type", "direct");
        return new CustomExchange(DELAYED_EXCHANGE, "x-delayed-message", true, false, arguments);
    }

    @Bean("delayedRoutingkey")
    public Binding delayedRoutingkey(Queue delayedQueue, CustomExchange delayedExchange) {
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYED_ROUTINGKEY).noargs();
    }
}
